package cn.iocoder.an.module.business.controller.admin.storemanage;

import cn.iocoder.an.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.controller.admin.storeinit.vo.DrawStoreInitImportVo;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.time.LocalDate;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.storemanage.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.storemanage.StoreManageDO;
import cn.iocoder.an.module.business.service.storemanage.StoreManageService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 出入库管理")
@RestController
@RequestMapping("/business/store-manage")
@Validated
public class StoreManageController {

    @Resource
    private StoreManageService storeManageService;

    @PostMapping("/create")
    @Operation(summary = "创建出入库管理")
    @PreAuthorize("@ss.hasPermission('business:store-manage:create')")
    public CommonResult<Long> createStoreManage(@Valid @RequestBody StoreManageSaveReqVO createReqVO) {
        return success(storeManageService.createStoreManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出入库管理")
    @PreAuthorize("@ss.hasPermission('business:store-manage:update')")
    public CommonResult<Boolean> updateStoreManage(@Valid @RequestBody StoreManageSaveReqVO updateReqVO) {
        storeManageService.updateStoreManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出入库管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:store-manage:delete')")
    public CommonResult<Boolean> deleteStoreManage(@RequestParam("id") Long id) {
        storeManageService.deleteStoreManage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除出入库管理")
                @PreAuthorize("@ss.hasPermission('business:store-manage:delete')")
    public CommonResult<Boolean> deleteStoreManageList(@RequestParam("ids") List<Long> ids) {
        storeManageService.deleteStoreManageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出入库管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:store-manage:query')")
    public CommonResult<StoreManageRespVO> getStoreManage(@RequestParam("id") Long id) {
        StoreManageDO storeManage = storeManageService.getStoreManage(id);
        return success(BeanUtils.toBean(storeManage, StoreManageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出入库管理分页")
    @PreAuthorize("@ss.hasPermission('business:store-manage:query')")
    public CommonResult<PageResult<StoreManageRespVO>> getStoreManagePage(@Valid StoreManagePageReqVO pageReqVO) {
        PageResult<StoreManageDO> pageResult = storeManageService.getStoreManagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreManageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出入库管理 Excel")
    @PreAuthorize("@ss.hasPermission('business:store-manage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStoreManageExcel(@Valid StoreManagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreManageDO> list = storeManageService.getStoreManagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出入库管理.xlsx", "数据", StoreManageRespVO.class,
                        BeanUtils.toBean(list, StoreManageRespVO.class));
    }




    @GetMapping("/get-import-template")
    @Operation(summary = "导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<StoreManageImportVo> list = Arrays.asList(
                StoreManageImportVo.builder().customerAccount("NCJL").drawingNumber("RS1-9461-BA")
                        .productName("废气进气管").operDate(LocalDate.of(2025, 1, 1)).inAmount(10).outAmount(20).build()
                );
        // 输出
        ExcelUtils.write(response, "导入模板.xlsx", "列表", StoreManageImportVo.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<StoreManageImportVo> list = ExcelUtils.read(file, StoreManageImportVo.class);
        return success(storeManageService.importList(list, updateSupport));
    }




}